'use strict';

const authConst = require('../constant/auth-const');
const dataConst = require('../constant/data-const');
const memberConst = require('../constant/member-const');
const sdkConst = require('../constant/sdk-const');

const Controller = require('egg').Controller;

class MemberController extends Controller {

    async weAppLife() {
        const { ctx } = this;
        const { lifeType, userId } = ctx.query;
        console.log('weAppLife:', lifeType, 'userId:', userId);
        ctx.successResult();
    }

    async initMemberInfo() {
        const { ctx } = this;

        ctx.successResult({
            dataType: {
                registerType: dataConst.registerType,
                verifyType: dataConst.verifyType,
                verifyStateType: dataConst.verifyStateType,
                singleType: dataConst.singleType,
                imageType: dataConst.imageType,
                meetingType: dataConst.meetingType
            },
            authInfo: {
                loginType: authConst.loginType,
                wxInfo: {
                    subKey: sdkConst.WinXin.mapSubKey
                }
            },
            baseInfo: {
                registerType: dataConst.registerType,
                verifyType: dataConst.verifyType,
                verifyStateType: dataConst.verifyStateType,
                singleType: dataConst.singleType,
                imageType: dataConst.imageType,
                genderInfo: memberConst.genderInfo,
                birthdayInfo: memberConst.birthdayInfo,
                initRegionInfo: memberConst.initRegionInfo,
                monthPayInfo: memberConst.monthPayInfo,
                educationInfo: memberConst.educationInfo,
                marriageStateInfo: memberConst.marriageStateInfo
            },
            otherInfo: {
                initRegionInfo: memberConst.initRegionInfo,
                statureInfo: memberConst.statureInfo,
                childStateInfo: memberConst.childStateInfo,
                organizationInfo: memberConst.organizationInfo,
                vocationInfo: memberConst.vocationInfo,
                houseStateInfo: memberConst.houseStateInfo,
                loanStateInfo: memberConst.loanStateInfo,
                vehicleStateInfo: memberConst.vehicleStateInfo,
                smokeStateInfo: memberConst.smokeStateInfo,
                drinkStateInfo: memberConst.drinkStateInfo,
                nationalityInfo: memberConst.nationalityInfo,
                havingStateInfo: memberConst.havingStateInfo
            },
            mateInfo: {
                registerType: dataConst.registerType,
                genderInfo: [
                    ...memberConst.noRequireInfo,
                    ...memberConst.genderInfo
                ],
                birthdayInfo: memberConst.birthdayInfo,
                initRegionInfo: memberConst.initRegionInfo,
                monthPayExpect: [
                    ...memberConst.noRequireInfo,
                    ...memberConst.monthPayExpect
                ],
                educationInfo: [
                    ...memberConst.noRequireInfo,
                    ...memberConst.educationInfo
                ],
                statureInfo: [
                    ...memberConst.noRequireInfo,
                    ...memberConst.statureInfo
                ],
                marriageStateInfo: [
                    ...memberConst.noRequireInfo,
                    ...memberConst.marriageStateInfo
                ],
                childStateInfo: [
                    ...memberConst.noRequireInfo,
                    ...memberConst.childStateInfo
                ],
                organizationInfo: [
                    ...memberConst.noRequireInfo,
                    ...memberConst.organizationInfo
                ],
                vocationInfo: [
                    ...memberConst.noRequireInfo,
                    ...memberConst.vocationInfo
                ],
                houseRequireInfo: [
                    ...memberConst.noRequireInfo,
                    ...memberConst.houseRequireInfo
                ],
                loanStateInfo: [
                    ...memberConst.noRequireInfo,
                    ...memberConst.loanStateInfo
                ],
                vehicleStateInfo: [
                    ...memberConst.noRequireInfo,
                    ...memberConst.vehicleStateInfo
                ],
                smokeStateInfo: [
                    ...memberConst.noRequireInfo,
                    ...memberConst.smokeStateInfo
                ],
                drinkStateInfo: [
                    ...memberConst.noRequireInfo,
                    ...memberConst.drinkStateInfo
                ],
                habitRequireInfo: [
                    ...memberConst.noRequireInfo,
                    ...memberConst.habitRequireInfo
                ],
                nationalityInfo: [
                    ...memberConst.noRequireInfo,
                    ...memberConst.nationalityInfo
                ],
                havingStateInfo: [
                    ...memberConst.noRequireInfo,
                    ...memberConst.havingStateInfo
                ]
            },
            labelInfo: memberConst.personLabelInfo
        });
    }

    async initUserSearchInfo() {
        const { ctx } = this;

        ctx.successResult({
            registerType: dataConst.registerType,
            genderInfo: [
                ...memberConst.noRequireInfo,
                ...memberConst.genderInfo
            ],
            birthdayInfo: memberConst.birthdayInfo,
            initRegionInfo: memberConst.initRegionInfo,
            monthPayExpect: [
                ...memberConst.noRequireInfo,
                ...memberConst.monthPayExpect
            ],
            educationInfo: [
                ...memberConst.noRequireInfo,
                ...memberConst.educationInfo
            ],
            statureInfo: [
                ...memberConst.noRequireInfo,
                ...memberConst.statureInfo
            ],
            marriageStateInfo: [
                ...memberConst.noRequireInfo,
                ...memberConst.marriageStateInfo
            ],
            childStateInfo: [
                ...memberConst.noRequireInfo,
                ...memberConst.childStateInfo
            ],
            organizationInfo: [
                ...memberConst.noRequireInfo,
                ...memberConst.organizationInfo
            ],
            vocationInfo: [
                ...memberConst.noRequireInfo,
                ...memberConst.vocationInfo
            ],
            houseStateInfo: [
                ...memberConst.noRequireInfo,
                ...memberConst.houseStateInfo
            ],
            drinkStateInfo: [
                ...memberConst.noRequireInfo,
                ...memberConst.drinkStateInfo
            ],
            nationalityInfo: [
                ...memberConst.noRequireInfo,
                ...memberConst.nationalityInfo
            ]
        });
    }

    async initUserFilterInfo() {
        const { ctx } = this;

        ctx.successResult({
            registerType: dataConst.registerType,
            verifyStateType: dataConst.verifyStateType,
            singleType: dataConst.singleType,
            genderInfo: memberConst.genderInfo,
            birthdayInfo: memberConst.birthdayInfo,
            monthPayInfo: memberConst.monthPayInfo,
            educationInfo: memberConst.educationInfo,
            statureInfo: memberConst.statureInfo,
            marriageStateInfo: memberConst.marriageStateInfo,
            childStateInfo: memberConst.childStateInfo,
            organizationInfo: memberConst.organizationInfo,
            vocationInfo: memberConst.vocationInfo,
            houseStateInfo: memberConst.houseStateInfo,
            loanStateInfo: memberConst.loanStateInfo,
            vehicleStateInfo: memberConst.vehicleStateInfo,
            smokeStateInfo: memberConst.smokeStateInfo,
            drinkStateInfo: memberConst.drinkStateInfo,
            nationalityInfo: memberConst.nationalityInfo,
            havingStateInfo: memberConst.havingStateInfo
        });
    }

    async getWeiXinUserSession() {
        const { ctx, service } = this;
        const { code } = ctx.params;

        try {
            const result = await service.wechat.getWeiXinUserSessionSrv(code);
            ctx.successResult(result);
        } catch (error) {
            return ctx.errorResult(error.message);
        }
    }

    async getWeiXinPhoneNumber() {
        const { ctx, service } = this;
        const { code, encryptedData, iv } = ctx.query;

        try {
            const { session_key } = await service.wechat.getWeiXinUserSessionSrv(code);
            const result = await service.wechat.getWeiXinDecryptData(session_key, encryptedData, iv);
            ctx.successResult(result);
        } catch (error) {
            return ctx.errorResult(error.message);
        }
    }

    async sendVerifyCode() {
        const { ctx } = this;
        const { body } = ctx.request;
        const { phone } = body;

        if (!phone) {
            return ctx.errorResult('phone missing');
        }

        let smsCode = await ctx.helper.getRedisStr(`register:${phone}`);
        if (smsCode) {
            ctx.errorResult(`验证码${smsCode}已发送。`);
        } else {
            smsCode = ctx.helper.generateRandomAlphaNum(4);
            await ctx.helper.setRedisStr(`register:${phone}`, smsCode, 60);
            ctx.successResult({}, `验证码是${smsCode}，1分钟内有效。`);
        }
    }

    async memberRegister() {
        const { ctx, service } = this;
        const { body } = ctx.request;
        const { username, password, smsCode, wxCode, nickname, gender } = body;

        if (!wxCode) {
            return ctx.errorResult('wechat code missing');
        }

        if (!username) {
            return ctx.errorResult('phone missing');
        }

        if (smsCode) {
            const cacheSmsCode = await ctx.helper.getRedisStr(`register:${username}`);
            if (smsCode !== cacheSmsCode) {
                return ctx.errorResult('sms code mismatch');
            }
        } else {
            return ctx.errorResult('sms code missing');
        }

        if (!password) {
            return ctx.errorResult('password missing');
        }

        try {
            const { openid } = await service.wechat.getWeiXinUserSessionSrv(wxCode);

            const userData = {
                username,
                password,
                third_id: openid,
                nickname,
                gender,
                phone: username,
                type: authConst.userType.user.value
            };

            const roleResult = await service.user.getAppUserRoleSrv();
            if (roleResult) {
                userData.role_id = roleResult.id
            }

            const registerResult = await service.user.addUserSrv(userData);
            if (registerResult) {
                const { dataValues } = registerResult;
                const { id, username, password, type, role_id } = dataValues;
                const authToken = ctx.helper.jwtSign({username, type, roleId: role_id});
                // await ctx.helper.setRedisStr(`user:${username}`, authToken);

                await service.member.initUserBaseSrv(id);
                await service.member.initUserMateSrv(id);
                const userResult = await service.member.getUserDetailSrv({userId: id});
                if (userResult) {
                    ctx.successResult({
                        ...userResult,
                        authToken
                    });
                } else {
                    ctx.errorResult('no user');
                }
            } else {
                ctx.errorResult('user exist');
            }
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async userLoginProcess(userData, longitude, latitude) {
        const { ctx, service } = this;

        const { id, username, type, role_id } = userData;
        userData.authToken = ctx.helper.jwtSign({username, type, roleId: role_id});
        // await ctx.helper.setRedisStr(`user:${username}`, authToken);

        userData.userMate = await service.member.getUserMateSrv(id);
        userData.userPhoto = await service.image.getMemberPhotoListSrv(id);
        userData.userLabel = await service.userLabel.getUserLabelListSrv(id);
        userData.userVerify = await service.verify.getUserVerifyStateSrv(id);
        userData.complaintState = await service.verify.getMemberComplaintStateSrv(id);
        const firstLogin = await service.record.recordLoginHistorySrv(id, longitude, latitude);
        if (firstLogin) {
            userData.bonus_points = await service.member.updateUserBonusPointsSrv(id);
        }
        await service.member.editUserBaseSrv(id, {online: true});

        return userData;
    }

    async memberLogin() {
        const { ctx, service } = this;
        const { body } = ctx.request;
        const { loginType, username, password, userId, openId, longitude, latitude } = body;

        switch (loginType) {
            case authConst.loginType.passwordLogin:
                if (!username) {
                    return ctx.errorResult('phone missing');
                }
                if (!password) {
                    return ctx.errorResult('password missing');
                }
                break;

            case authConst.loginType.userIdLogin:
                if (!userId) {
                    return ctx.errorResult('userId missing');
                }
                break;

            case authConst.loginType.openIdLogin:
                if (!openId) {
                    return ctx.errorResult('openId missing');
                }
                break;
        }

        try {
            const result = await service.member.getUserDetailSrv({username, password, userId, thirdId: openId});
            if (result) {
                const loginResult = await this.userLoginProcess(result, longitude, latitude);
                ctx.successResult(loginResult);
            } else {
                ctx.errorResult('no user');
            }
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async wxLogin() {
        const { ctx, service } = this;
        const { body } = ctx.request;
        const { code, longitude, latitude } = body;


        try {
            const { openid, errmsg } = await service.wechat.getWeiXinUserSessionSrv(code);
            if (errmsg) {
                return ctx.errorResult(errmsg);
            } else if (!openid) {
                return ctx.errorResult('can not get openid');
            }

            const result = await service.member.getUserDetailSrv({thirdId: openid});
            if (result) {
                const loginResult = await this.userLoginProcess(result, longitude, latitude);
                ctx.successResult(loginResult);
            } else {
                ctx.errorResult('no user');
            }
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async memberLogout() {
        const { ctx, service } = this;
        const { userId } = ctx.params;

        try {
            const result = await service.member.editUserBaseSrv(userId, {online: false});
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async updateMemberOnlineState() {
        const { ctx, service } = this;
        const { userId } = ctx.params;
        const { body } = ctx.request;
        const { onlineState } = body;

        try {
            const result = await service.member.editUserBaseSrv(userId, {online: onlineState});
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async getMemberDetailById() {
        const { ctx, service } = this;
        const { userId } = ctx.params;

        try {
            const result = await service.member.getUserDetailSrv({userId});
            if (result) {
                result.userVerify = await service.verify.getUserVerifyStateSrv(userId);
                result.complaintState = await service.verify.getMemberComplaintStateSrv(userId);
                ctx.successResult(result);
            } else {
                ctx.errorResult('no user');
            }
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async getMemberList() {
        const { ctx, service } = this;
        const { limit, offset, orderName, orderType, ...queryData } = ctx.query;

        try {
            const queryBody = {
                queryData,
                limit,
                offset,
                orderName,
                orderType
            };
            const result = await service.member.getUserListSrv(queryBody);
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async getMemberAllInfoList() {
        const { ctx, service } = this;
        const { limit, offset, orderName, orderType, ...queryData } = ctx.query;

        try {
            const queryBody = {
                queryData,
                limit,
                offset,
                orderName,
                orderType
            };
            const result = await service.member.getUserAllInfoListSrv(queryBody);
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async getVerifyApplyList() {
        const { ctx, service } = this;
        const { verifyType, verifyState, limit, offset } = ctx.query;

        try {
            const result = await service.verify.getVerifyApplyListSrv({verifyType, verifyState, limit, offset});
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async editMemberInfo() {
        const { ctx, service } = this;
        const { userId } = ctx.params;
        const { body } = ctx.request;

        const { nickname, gender, birthday, bodyHeight, liveRegion, monthPay, education, marriageState, registerState } = body;
        const userData = { nickname };

        const userBaseData = {
            body_height: bodyHeight,
            live_region: liveRegion,
            month_pay: monthPay,
            marriage_state: marriageState
        };

        try {
            if (registerState < dataConst.registerType.registered.value) {
                userData.gender = gender;
                userData.birthday = birthday;
                userBaseData.education_id = education;
                userBaseData.marriage_state = marriageState;
                userBaseData.register_state = dataConst.registerType.registered.value;

                const [male, female] = memberConst.genderInfo;
                const userMateData = {
                    gender: gender > male.value ? male.value : female.value
                };

                await service.member.editUserSrv(userId, userData);
                await service.member.editUserBaseSrv(userId, userBaseData);
                await service.member.editUserMateSrv(userId, userMateData);
            } else {
                await service.member.editUserSrv(userId, userData);
                await service.member.editUserBaseSrv(userId, userBaseData);
            }

            const result = await service.member.getUserDetailSrv({userId});
            result.userVerify = await service.verify.getUserVerifyStateSrv(userId);
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async editMemberOther() {
        const { ctx, service } = this;
        const { userId } = ctx.params;
        const { body } = ctx.request;

        const {
            birthRegion, bodyWeight, childState, drinkState, houseState, loanState,
            major, organization, vocation, smokeState, stature, university, vehicleState,
            housePlan, childPlan, nationality, introduction
        } = body;

        const userBaseData = {
            body_weight: bodyWeight,
            stature_id: stature,
            birth_region: birthRegion,
            child_state: childState,
            organization_id: organization,
            vocation_id: vocation,
            university: university,
            major: major,
            house_state: houseState,
            loan_state: loanState,
            vehicle_state: vehicleState,
            smoke_state: smokeState,
            drink_state: drinkState,
            house_plan: housePlan,
            child_plan: childPlan,
            nationality_id: nationality,
            introduction
        };

        try {
            await service.member.editUserBaseSrv(userId, userBaseData);
            const result = await service.member.getUserDetailSrv({userId});
            result.userVerify = await service.verify.getUserVerifyStateSrv(userId);
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async getMemberMate() {
        const { ctx, service } = this;
        const { userId } = ctx.params;

        try {
            const result = await service.member.getUserMateSrv(userId);
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async editMemberMate() {
        const { ctx, service } = this;
        const { userId } = ctx.params;
        const { body } = ctx.request;

        const {
            gender, liveRegion, birthRegion, minAge, maxAge, minHeight, maxHeight, minWeight, maxWeight,
            monthPay, education, organization, vocation, stature, houseState, loanState, vehicleState,
            marriageState, childState, smokeState, drinkState, housePlan, childPlan, nationality
        } = body;

        const userMateData = {
            gender,
            min_age: minAge,
            max_age: maxAge,
            min_height: minHeight,
            max_height: maxHeight,
            min_weight: minWeight,
            max_weight: maxWeight,
            stature_id: stature,
            live_region: liveRegion,
            birth_region: birthRegion,
            month_pay: monthPay,
            education_id: education,
            organization_id: organization,
            vocation_id: vocation,
            house_state: houseState,
            loan_state: loanState,
            vehicle_state: vehicleState,
            marriage_state: marriageState,
            child_state: childState,
            smoke_state: smokeState,
            drink_state: drinkState,
            house_plan: housePlan,
            child_plan: childPlan,
            nationality_id: nationality
        };

        try {
            await service.member.editUserMateSrv(userId, userMateData);
            const result = await service.member.getUserMateSrv(userId);
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async getFriendMember() {
        const { ctx, service } = this;
        const { targetUserId, userId } = ctx.params;

        try {
            const userResult = await service.member.getUserDetailSrv({userId: targetUserId});
            if (userResult) {
                const result = {
                    userInfo: userResult
                };

                result.mateInfo = await service.member.getUserMateSrv(targetUserId);
                result.userPhoto = await service.image.getMemberPhotoListSrv(targetUserId);
                result.userLabel = await service.userLabel.getUserLabelListSrv(targetUserId);
                result.userVerify = await service.verify.getUserVerifyStateSrv(targetUserId);
                result.relation = await service.memberRelation.getMemberRelationSrv(userId, targetUserId);
                result.followed = await service.memberRelation.getMemberFollowedCount(targetUserId);
                result.coupled = await service.memberCouple.getMemberCoupleRelationSrv(userId, targetUserId);
                result.latestLogin = await service.record.getLatestLoginDateSrv(targetUserId);
                if (targetUserId !== userId) {
                    await service.record.recordUserLookMemberSrv(userId, targetUserId);
                }

                ctx.successResult(result);
            } else {
                ctx.errorResult('no user');
            }
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async getFriendSimpleById() {
        const { ctx, service } = this;
        const { targetUserId, userId } = ctx.params;

        try {
            const userResult = await service.member.getUserSimpleSrv(targetUserId);
            if (userResult) {
                const relation = await service.memberRelation.getMemberRelationSrv(userId, targetUserId);
                ctx.successResult({
                    ...userResult,
                    relation
                });
            } else {
                ctx.errorResult('no user');
            }
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async getMemberRelation() {
        const { ctx, service } = this;
        const { userId, targetUserId } = ctx.params;

        try {
            const result = await service.memberRelation.getMemberRelationSrv(userId, targetUserId);
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async addMemberRelation() {
        const { ctx, service } = this;
        const { userId, targetUserId } = ctx.params;
        const { body } = ctx.request;
        const { relationType } = body;

        try {
            const result = await service.memberRelation.addMemberRelationSrv(userId, targetUserId, relationType);
            if (result) {
                ctx.successResult(result);
            } else {
                ctx.errorResult('member already followed');
            }
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async removeMemberRelation() {
        const { ctx, service } = this;
        const { relationId } = ctx.params;

        try {
            const result = await service.memberRelation.removeMemberRelationSrv(relationId);
            if (result > 0) {
                ctx.successResult();
            } else {
                ctx.errorResult('cancel follow failed');
            }
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async getMemberRelationList() {
        const { ctx, service } = this;
        const { userId } = ctx.params;
        const { relationType, limit, offset, orderName, orderType } = ctx.query;

        try {
            const queryBody = {
                userId,
                relationType,
                limit,
                offset,
                orderName,
                orderType
            };
            const result = await service.memberRelation.getMemberRelationListSrv(queryBody);
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async getMemberFollowedList() {
        const { ctx, service } = this;
        const { targetUserId } = ctx.params;
        const { relationType, limit, offset, orderName, orderType } = ctx.query;

        try {
            const queryBody = {
                targetUserId,
                relationType,
                limit,
                offset,
                orderName,
                orderType
            };
            const result = await service.memberRelation.getMemberFollowedListSrv(queryBody);
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async memberLookForList() {
        const { ctx, service } = this;
        const { userId } = ctx.params;
        const { limit, offset } = ctx.query;

        try {
            const result = await service.memberRelation.getMemberLookForListSrv(userId, limit, offset);
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async memberLookMeList() {
        const { ctx, service } = this;
        const { targetUserId } = ctx.params;
        const { limit, offset } = ctx.query;

        try {
            const result = await service.memberRelation.getMemberLookMeListSrv(targetUserId, limit, offset);
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async getMemberPhotoList() {
        const { ctx, service } = this;
        const { userId } = ctx.params;

        try {
            const result = await service.image.getMemberPhotoListSrv(userId);
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async deleteMemberPhoto() {
        const { ctx, service } = this;
        const { body } = ctx.request;
        const { userId, image } = body;

        if (!userId) {
            return ctx.errorResult('userId missing');
        }
        if (!image) {
            return ctx.errorResult('image missing');
        }

        try {
            const result = await service.image.removeMemberPhotoSrv(userId, image);
            console.log('deleteMemberPhoto:', result);
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async getMemberIdentity() {
        const { ctx, service } = this;
        const { userId } = ctx.params;

        try {
            const result = await service.image.getMemberIdentitySrv(userId);
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async deleteMemberIdentity() {
        const { ctx, service } = this;
        const { body } = ctx.request;
        const { userId, imageType, image } = body;

        if (!userId) {
            return ctx.errorResult('userId missing');
        }
        if (!imageType) {
            return ctx.errorResult('imageType missing');
        }
        if (!image) {
            return ctx.errorResult('image missing');
        }

        try {
            const result = await service.image.removeMemberIdentitySrv(userId, imageType, image);
            console.log('deleteMemberIdentity:', result);
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async getUserVerifyState() {
        const { ctx, service } = this;
        const { userId } = ctx.params;

        try {
            const result = await service.verify.getUserVerifyStateSrv(userId);
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async setMemberVerifyState() {
        const { ctx, service } = this;
        const { userId } = ctx.params;
        const { body } = ctx.request;
        const { verifyType, verifyState, content } = body;

        if (!verifyType) {
            return ctx.errorResult('verifyType missing');
        }
        if (!verifyState) {
            return ctx.errorResult('verifyState missing');
        }

        try {
            const result = await service.verify.setUserVerifyStateSrv(userId, verifyType, verifyState, content);
            if (result) {
                switch (verifyType) {
                    case dataConst.verifyType.avatar.value:
                        await service.member.editUserBaseSrv(userId, {avatar_verify: verifyState});
                        break;

                    case dataConst.verifyType.identity.value:
                        await service.member.editUserBaseSrv(userId, {identity_verify: verifyState});
                        break;

                    case dataConst.verifyType.education.value:
                        await service.member.editUserBaseSrv(userId, {education_verify: verifyState});
                        break;
                }
                ctx.successResult(result);
            } else {
                ctx.errorResult('verify failed');
            }
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async addMemberLabelList() {
        const { ctx, service } = this;
        const { userId } = ctx.params;
        const { body } = ctx.request;
        const { labelArray } = body;

        try {
            const result = await service.userLabel.addUserLabelListSrv(userId, labelArray);
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async getMemberLabelList() {
        const { ctx, service } = this;
        const { userId } = ctx.params;

        try {
            const result = await service.userLabel.getUserLabelListSrv(userId);
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async createMemberComplaint() {
        const { ctx, service } = this;
        const { userId, targetUserId } = ctx.params;
        const { body } = ctx.request;
        const { content, image } = body;

        try {
            const result = await service.verify.createMemberComplaintSrv(userId, targetUserId, content, image);
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async verifyMemberComplaint() {
        const { ctx, service } = this;
        const { complaintId } = ctx.params;
        const { body } = ctx.request;
        const { verifyState, replay } = body;

        if (!verifyState) {
            return ctx.errorResult('verifyState mission');
        }
        if (!replay) {
            return ctx.errorResult('replay mission');
        }

        try {
            const result = await service.verify.verifyMemberComplaintSrv(complaintId, verifyState, replay);
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async getMemberComplaintState() {
        const { ctx, service } = this;
        const { userId } = ctx.params;

        try {
            const result = await service.verify.getMemberComplaintStateSrv(userId);
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async getMemberComplaintList() {
        const { ctx, service } = this;
        const { limit, offset, ...queryData } = ctx.query;

        try {
            const result = await service.verify.getMemberComplaintListSrv(queryData, limit, offset);
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async getMemberVerifyMessage() {
        const { ctx, service } = this;
        const { userId } = ctx.params;

        try {
            const result = await service.verify.getUserVerifyResultSrv(userId);
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async sendCoupleInvite() {
        const { ctx, service } = this;
        const { userId, targetUserId } = ctx.params;

        try {
            const result = await service.memberCouple.sendCoupleInviteSrv(userId, targetUserId);
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async receiveCoupleInvite() {
        const { ctx, service } = this;
        const { userId, targetUserId } = ctx.params;

        try {
            const result = await service.memberCouple.receiveCoupleInviteSrv(userId, targetUserId);
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async receiveCoupleInviteList() {
        const { ctx, service } = this;
        const { userId } = ctx.params;
        const { limit, offset } = ctx.query;

        try {
            const result = await service.memberCouple.receiveCoupleInviteListSrv({userId, limit, offset});
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async sendCoupleInviteList() {
        const { ctx, service } = this;
        const { userId } = ctx.params;
        const { limit, offset } = ctx.query;

        try {
            const result = await service.memberCouple.sendCoupleInviteListSrv({userId, limit, offset});
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async checkCoupleBindStatus() {
        const { ctx, service } = this;
        const { userId } = ctx.params;

        try {
            const result = await service.memberCouple.checkCoupleBindStatusSrv(userId);
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async checkTargetCoupleInvite() {
        const { ctx, service } = this;
        const { userId, targetUserId } = ctx.params;

        try {
            const result = await service.memberCouple.checkTargetCoupleInviteSrv(userId, targetUserId);
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async getCoupleMemberInfo() {
        const { ctx, service } = this;
        const { userId, targetUserId } = ctx.params;

        try {
            const self = await service.member.getUserSimpleSrv(userId);
            const target = await service.member.getUserSimpleSrv(targetUserId);
            const bindDate = await service.memberCouple.getCoupleBindDateTime(userId, targetUserId);
            ctx.successResult({self, target, bindDate});
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async cancelCoupleInvite() {
        const { ctx, service } = this;
        const { userId, targetUserId } = ctx.params;

        try {
            const result = await service.memberCouple.cancelCoupleInviteSrv(userId, targetUserId);
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async rejectCoupleInvite() {
        const { ctx, service } = this;
        const { userId, targetUserId } = ctx.params;

        try {
            const result = await service.memberCouple.rejectCoupleInviteSrv(userId, targetUserId);
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async releaseCoupleRelation() {
        const { ctx, service } = this;
        const { userId, targetUserId } = ctx.params;

        try {
            const result = await service.memberCouple.releaseCoupleRelationSrv(userId, targetUserId);
            ctx.successResult(result || null);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async getCoupleInviteCount() {
        const { ctx, service } = this;
        const { userId } = ctx.params;

        try {
            const result = await service.memberCouple.getCoupleInviteCountSrv(userId);
            ctx.successResult(result);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }

    async syncUserData() {
        const { ctx, service } = this;
        const { userId } = ctx.params;

        try {
            const dataResult = {};
            dataResult.userVerify = await service.verify.getUserVerifyStateSrv(userId);
            const bindStatus = await service.memberCouple.checkCoupleBindStatusSrv(userId);
            if (bindStatus) {
                dataResult.inviteInfo = 'CP中';
            } else {
                const inviteCount = await service.memberCouple.getCoupleInviteCountSrv(userId);
                if (inviteCount > 0) {
                    dataResult.inviteInfo = `${inviteCount}个邀请`;
                }
            }
            ctx.successResult(dataResult);
        } catch (error) {
            ctx.errorResult(error.message);
        }
    }
}

module.exports = MemberController;
